package com.yanghs.common.dao;

import com.yanghs.common.util.SerializeUtil;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.exceptions.JedisException;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
 * @author yanghs
 * @Description:对redis简单封装
 * @date 2018/3/21 21:11
 */
public class RedisDao {
    private static final Logger log = LoggerFactory.getLogger(RedisDao.class);
    private ShardedJedisPool jedisPool;
    public ShardedJedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(ShardedJedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }
    public ShardedJedis getJedis(){
        ShardedJedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
        }catch (JedisException e){
            log.debug("从redis连接池得到连接出错");
            e.printStackTrace();
        }
        return jedis;
    }
    public byte[] getValueByKey(byte[] key) throws Exception {
        ShardedJedis jedis = null;
        byte[] result = null;
        try {
            jedis = getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return result;
    }
    public void saveValueByKey( byte[] key, byte[] value, int expireTime)throws Exception {
        ShardedJedis jedis = null;
        byte[] result = null;
        try {
            jedis = getJedis();
            jedis.set(key, value);
            if (expireTime > 0)
                jedis.expire(key, expireTime);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
    public void deleteByKey(byte[] key) throws Exception {
        ShardedJedis jedis = null;
        try {
            jedis = getJedis();
            Long result = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
    public void deleteAll()throws Exception{
        Iterator<Jedis> jedisIterator = getJedis().getAllShards().iterator();
        while(jedisIterator.hasNext()){
            Jedis jedis = jedisIterator.next();
            jedis.flushAll();
        }
    }
    public Collection getLikeKeys(String pattern,Class clazz){
        Collection sessions = new HashSet();
        Collection jedisCollection = getJedis().getAllShards();
        Iterator<Jedis> jedisIterator = jedisCollection.iterator();
        Jedis jedis;
        while (jedisIterator.hasNext()){
            jedis = jedisIterator.next();
            /*获得key的集合*/
            Set<byte[]> bytekeys =  jedis.keys(pattern.getBytes());
            for (byte[] key:bytekeys) {
                Object o = SerializeUtil.deserialize(jedis.get(key),clazz);
                if (o instanceof Session){
                    sessions.add(o);
                }
            }
        }
        return sessions;
    }

}
